﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace MapLibrary
{
    /// <summary>
    /// Panel on the LevelEditor that will modify the Palette.
    /// </summary>
    class PaletteEditorPanel
    {
        private const int OFFSET_Y = 10;
        private const int OFFSET_X = 25;
        private const int PADDING_X = 10;
        private const float ICON_WIDTH = 60.0f;
        private const float ICON_HEIGHT = 60.0f;

        private const int NUMBER_PER_PAGE = 8;

        private Palette mPalette;
        private ContentManager mContentManager;
        private List<Texture2D> mPaletteTextures;

        private Texture2D mSelectTexture;

        private int mCurrentSelection;

        private bool mPanelEnabled;

        private SpriteFont mFont;

        private SelectionManager<Texture2D> mSelectionManager;

        public PaletteEditorPanel()
        {
            mPaletteTextures = new List<Texture2D>();
            mCurrentSelection = 0;
            mPanelEnabled = true;
            mSelectionManager = new SelectionManager<Texture2D>(
                mPaletteTextures,
                NUMBER_PER_PAGE);
        }

        public void Load(ContentManager pContentManager)
        {
            //load the necessary textures for the panel
            //ie save, load, page turn, background, etc.
            mContentManager = pContentManager;

            mSelectTexture = pContentManager.Load<Texture2D>(@"./PalettePanel/palettePanelSelect");
            mFont = pContentManager.Load<SpriteFont>(@"./PalettePanel./PalettePanelFont");
        }

        /// <summary>
        /// Initializes the panel with the provided palette.
        /// </summary>
        /// <param name="pPalette"></param>
        public void LoadPalette(Palette pPalette)
        {
            mPalette = pPalette;

            LoadPaletteTextures();
        }

        public void Disable()
        {
            mPaletteTextures.Clear();
        }

        public bool Enabled
        {
            get { return mPanelEnabled; }
            set { mPanelEnabled = value; }
        }

        /// <summary>
        /// Assumed that pText is a texture that is contained in the palette!
        /// </summary>
        /// <param name="pText"></param>
        /// <returns></returns>
        public string GetAssetNameForTexture(Texture2D pText)
        {
            int index = mPaletteTextures.IndexOf(pText);

            return mPalette.TextureAssets[index];
        }

        private Vector2 GetScaleForTexture(Rectangle bounds)
        {
            Vector2 tScale = new Vector2();
            tScale.X = ICON_WIDTH / bounds.Width;
            tScale.Y = ICON_HEIGHT / bounds.Height;
            return tScale;
        }

        /// <summary>
        /// Draw the panel on the top of the screen.
        /// </summary>
        /// <param name="pSpriteBatch"></param>
        public void Draw(SpriteBatch pSpriteBatch)
        {
            int totalXOffset = OFFSET_X;
            int textureCount = 0;
            foreach (Texture2D texture in mSelectionManager.CurrentPage)
            {
                Rectangle rect = texture.Bounds;

                pSpriteBatch.Draw(
                    texture,
                    new Vector2(totalXOffset, OFFSET_Y),
                    null,
                    Color.White,
                    0,
                    Vector2.Zero,
                    GetScaleForTexture(rect),
                    SpriteEffects.None,
                    0f);

                if (textureCount == mSelectionManager.SelectionIndex)
                {
                    pSpriteBatch.Draw(
                        mSelectTexture,
                        new Vector2(totalXOffset, OFFSET_Y),
                        null,
                        Color.White,
                        0,
                        Vector2.Zero,
                        new Vector2(1.0f,1.0f),
                        SpriteEffects.None,
                        0f);
                }

                textureCount++;

                totalXOffset += (int)ICON_WIDTH + PADDING_X;
            }

            pSpriteBatch.DrawString(
                mFont,
                mSelectionManager.CurrentPageNumber + " (" + mSelectionManager.NumberOfPages + ")",
                new Vector2(OFFSET_X, 2 * OFFSET_Y + 60),
                Color.White);
        }

        public void MoveSelectionLeft()
        {
            if(Enabled)
                mSelectionManager.MoveSelectionLeft();
        }

        public void MoveSelectionRight()
        {
            if(Enabled)
                mSelectionManager.MoveSelectionRight();
        }

        public Texture2D GetSelectedTexture()
        {
            if (!mPanelEnabled)
                return null;

            if (mCurrentSelection == 0)
                if (mPaletteTextures.Count > 0)
                    return mSelectionManager.Selection;
                else
                    return null;
            else
                return mSelectionManager.Selection;
        }

        private void LoadPaletteTextures()
        {
            foreach (string Asset in mPalette.TextureAssets)
            {
                mPaletteTextures.Add(mContentManager.Load<Texture2D>(Asset));
            }
        }
    }
}
